<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use think\Log;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Borrowerinfo extends Backend
{
    
    /**
     * Borrow模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Borrow');

    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            $filter = $this->request->get("filter", '');
            $create = json_decode($filter,true);
            $wheres = '';
            $wheres1 = '';
            if($create){
                $times = explode(',',$create['createTime']);

                $wheres = ' and createTime >= '.$times[0].' and createTime <='.$times[1];
                $wheres1 = ' and updateTime >= '.$times[0].' and updateTime <='.$times[1];

            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)->group('userId')
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)->group('userId')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $key => $val){

                    $user = Db::name('user')->where('userId',$val['userId'])->find();
                    $list[$key]['username'] = $user['name'];
                    $list[$key]['phone'] = $user['phone'];

                    $fabiao_count = Db::name('borrow')->field('count(borrowId) as count,sum(account) as total')->where(' userId = '.$val['userId']. ' and status !=0 and status !=2 '.$wheres)->find();
                    $list[$key]['fabiao_count'] =$fabiao_count['count'];
                    $list[$key]['fabiao_total'] =$fabiao_count['total'];

                    $manbiao_count = Db::name('borrow')->field('count(borrowId) as count,sum(account) as total')->where(' userId = '.$val['userId']. ' and status >3 '.$wheres)->find();
                    $list[$key]['manbiao_count'] =$manbiao_count['count'];
                    $list[$key]['manbiao_total'] =$manbiao_count['total'];

                $tixian_count = Db::name('cash')->field('sum(money) as total')->where(' userId = '.$val['userId']. ' and status =1 '.$wheres1)->find();

                $list[$key]['cash_total'] =$tixian_count['total'];


            }




            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    

}
